package com.health.work.other;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.health.work.utils.BitmapBase64;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.UploadUtil;
import com.health.work.utils.WebServiceUtils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.Toast;

public class CameraUtils {
	private String CameraUtilsTag = "CameraUtils";
	/*用来标识请求照相功能的activity*/
	private static final int CAMERA_WITH_DATA = 3023;

	/*用来标识请求gallery的activity*/
	private static final int PHOTO_PICKED_WITH_DATA = 3021;

	/*拍照的照片存储位置*/
	private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera");

	public File mCurrentPhotoFile;//照相机拍照得到的图片
	
	public  void doPickPhotoAction(Activity context) {
		final Activity mActivity=context;
		// Wrap our context to inflate list items using correct theme
		final Context dialogContext = new ContextThemeWrapper(context,
				android.R.style.Theme_Light);
		String cancel="返回";
		String[] choices;
		choices = new String[2];
		choices[0] = "拍照";  //拍照
		choices[1] = "从相册中选择";  //从相册中选择
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);
	
		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle("选择方式");
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						switch (which) {
						case 0:{
							String status=Environment.getExternalStorageState();
							if(status.equals(Environment.MEDIA_MOUNTED)){//判断是否有SD卡
								doTakePhoto(mActivity);// 用户点击了从照相机获取
							}
							else{
//								showToast("没有SD卡");
								Toast.makeText(mActivity, "出现异常",
										Toast.LENGTH_LONG).show();
							}
							break;
							
						}
						case 1:
							doPickPhotoFromGallery(mActivity);// 从相册中去获取
							break;
						}
					}
				});
		builder.setNegativeButton(cancel, new DialogInterface.OnClickListener() {
	
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
			
		});
		builder.create().show();
	}


/**
* 拍照获取图片
* 
*/
public void doTakePhoto(Activity context) {
	try {
		// Launch camera to take photo for selected contact
		PHOTO_DIR.mkdirs();// 创建照片的存储目录
		mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName());// 给新照的照片文件命名
		final Intent intent = getTakePickIntent(mCurrentPhotoFile);
		context.startActivityForResult(intent, CAMERA_WITH_DATA);
	} catch (ActivityNotFoundException e) {
		Toast.makeText(context, "出现异常",
				Toast.LENGTH_LONG).show();
	}
}

public static Intent getTakePickIntent(File f) {
	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
	intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
	return intent;
}

/**
* 用当前时间给取得的图片命名
* 
*/
public String getPhotoFileName() {
	Date date = new Date(System.currentTimeMillis());
	SimpleDateFormat dateFormat = new SimpleDateFormat(
			"'IMG'_yyyy-MM-dd HH:mm:ss");
	return dateFormat.format(date) + ".jpg";
}

// 请求Gallery程序
public void doPickPhotoFromGallery(Activity context) {
	try {
		// Launch picker to choose photo for selected contact
		final Intent intent = getPhotoPickIntent();
		context.startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
	} catch (ActivityNotFoundException e) {
		Toast.makeText(context, "出现异常",
				Toast.LENGTH_LONG).show();
	}
}

// 封装请求Gallery的intent
public static Intent getPhotoPickIntent() {
	Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
	intent.setType("image/*");
	intent.putExtra("crop", "true");
	intent.putExtra("aspectX", 1);
	intent.putExtra("aspectY", 1);
	intent.putExtra("outputX", 80);
	intent.putExtra("outputY", 80);
	intent.putExtra("return-data", true);
	return intent;
}

public void doCropPhoto(Activity activity,File f) {
	try {
		// 启动gallery去剪辑这个照片
		final Intent intent = getCropImageIntent(Uri.fromFile(f));
		activity.startActivityForResult(intent, PHOTO_PICKED_WITH_DATA);
	} catch (Exception e) {
		Toast.makeText(activity, "出现异常",
				Toast.LENGTH_LONG).show();
	}
}

/**
* Constructs an intent for image cropping. 调用图片剪辑程序
*/
public Intent getCropImageIntent(Uri photoUri) {
	Intent intent = new Intent("com.android.camera.action.CROP");
	intent.setDataAndType(photoUri, "image/*");
	intent.putExtra("crop", "true");
	intent.putExtra("aspectX", 1);
	intent.putExtra("aspectY", 1);
	intent.putExtra("outputX", 80);
	intent.putExtra("outputY", 80);
	intent.putExtra("return-data", true);
	return intent;
}

}
